package Youdao.Test;

import Vocabulary.Mapper.WordMapper;
import Vocabulary.MybatisUtils;
import Vocabulary.W;
import Vocabulary.Word;
import com.sun.speech.freetts.Voice;
import com.sun.speech.freetts.VoiceManager;
import org.apache.ibatis.session.SqlSession;
import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;
import org.jsoup.nodes.Element;
import org.jsoup.select.Elements;
import org.junit.jupiter.api.Test;

import java.net.MalformedURLException;
import java.net.URL;
import java.util.ArrayList;
import java.util.List;

import static Vocabulary.MybatisUtils.convertListToByteArray;
import static Youdao.Test.Test.textToSpeech;
import static java.lang.Thread.sleep;

public class Main  {
    SqlSession sqlSession = MybatisUtils.getSqlSession();
    WordMapper wordMapper = sqlSession.getMapper(WordMapper.class);
    @Test
    public void Main() throws Exception {
        List<Word> words = wordMapper.getWords();
        List<List<byte[]>>information=new ArrayList<>();
        int i=0;
        for (Word word:words){
            Document parse = Jsoup.parse(new URL("https://www.youdao.com/result?word=lj%3A" + word.getWords() + "&lang=en"), 6000);
//            sleep(500);                              https://www.youdao.com/result?word=lj%3Agreat&lang=en
            Elements elementsByClass = parse.getElementsByClass("sen-eng");
            List<byte[]>information1=new ArrayList<>();
            Word word1 = wordMapper.GetWordByKey(word.getWords());
            for (Element element:elementsByClass){
//                information.add(element.text().getBytes());
//                System.out.println(element.text().getBytes()+"\n\n\n\n");
                information1.add(element.text().getBytes());
            }
            System.out.println(i);
            i++;
            byte[] bytes = convertListToByteArray(information1);
            wordMapper.updat0(word1.getWords(),bytes);
        }
    }
    @Test
    public void playMp3(){
        String s = "Rainfall is plentiful in the area." +
                "Our food, such as it is, is plentiful." +
                "She survived because of a plentiful supply of fresh water.";
        textToSpeech(s);

    }
}
